<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit" :showHistoryBtn="true">
		<om-autoform ref="autoform" :checkedit="checkedit"  :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>
<script>
import {
	formValidation,
	changeEditor,
	formShowLoading,
	configFormEditor,
	getWorkCacheKey,
	changeConfigAttr,
	configSelectData,
	formModelLoad,
	getFormFields
} from '@/static/js/formUtil.js';
import dateTimePicker from '@/static/js/dateTimePicker.js';
import urlUtil from '@/static/js/urlUtil.js';
import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
export default {
	provide() {
		return {
			formPage: this
		};
	},
	data() {
		return {
			checkedit:false,
			menuName: '',
			busCode: '',
			formdata: {},
			mainConfigData: [],
			subConfigData: {},
			initValue: {
				reviewLeaderInstId:"",
				reviewLeaderInstName:""
			},
			subEditorObj: {}, //存放子表编辑器数据
			projectCacheKey: 'des-cont-review-project-cache',
			reviewLeaderCacheKey: 'des-cont-review-reviewleader-cache',
			partakeUserCacheKey:'des-cont-review-partakeuser-cache'
		};
	},
	computed: {
		...mapState(useLoginUserStore, ['userName', 'userId'])
	},
	methods: {
		formSubmit() {
			if(this.checkedit){
				this.om.formSave('designReview/doReviewUpdate', this.formdata, this);
			}else{
				this.om.formSave('designReview/doAdd', this.formdata, this);
			}
		},
		//验证交付物
		verifyFileName(){
			if(!this.formdata.projectId){
				return "请选择“项目编码”"
			}
			return true
		},
		getWorkParam(){
			return {projectId:this.formdata.projectId}
		},
		initLoad(options) {
			let _that = this;
			var ajaxArr = [
				urlUtil.zposUrl(options.code), //制表岗位
				urlUtil.sysdictComboboxUrl('38211'), //评审方式
				urlUtil.sysdictComboboxUrl('38499')//评审结论相关信息
			];
			var fileId = options.fileId
			if(fileId){
				//评审结论相关信息
				ajaxArr.push({
					url:'designReview/getValue',
					param:{
						fileId:fileId
					}
				})
			}
			formModelLoad(ajaxArr, function(res, mainData) {
				changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', 'posname'), mainData);
				changeEditor('title1','projectId',{
					link: '../../public/pageSearch/pageSearch',
					linkParam: {
						initload: true,
						namefield: 'projectCode',
						notefield:'startDate~endDate',
						notesplit:'~',
						extrafield:'projectName',
						searchfield: 'q',
						url: "designReview/likeSerchShow",
						cachename: _that.projectCacheKey,
						placeholder: '请输入项目名称、编码、性质'
					}
				}, null, mainData);
				changeEditor('title5', 'reviewTypeId', 'data', configSelectData(res[1], 'id', 'dicname'), mainData);
				changeEditor('title5','reviewLeaderId',{//评审负责人
					link: '../../public/pageSearch/pageSearch',
					linkParam: {
						namefield: 'userPosInstName',
						searchfield: 'q',
						url: urlUtil.officePageUrl().url,
						cachename: _that.reviewLeaderCacheKey,
						placeholder: urlUtil.officePageUrl().prompt
					}
				}, null, mainData);
				//主要设计人
				changeEditor('title1','partakeUserIdStr',{
					linkParam: {
						namefield:"userPosInstName",
						searchfield:"q",
						url:urlUtil.userPageUrl().url,
						cachename:_that.partakeUserCacheKey,
						placeholder:urlUtil.officePageUrl().prompt,
						pagination:true,
					}
				}, null, mainData);
				if (_that.subEditorObj['title6']) {
					changeEditor('title6', 'fileStr', {
						getWorkParam:'getWorkParam',
							titleTemplate: '<%= fileName %>',
							subtitle1Template: '图号：<%= drawingCode %>',
							subtitle2Template: '数量：<%= num %>',
						},null,mainData);
				}
				if (_that.subEditorObj['title7']) {
					changeEditor('title7', 'traitStr', {
							titleTemplate: '图号：<%= drawingCode %>',
							subtitle1Template: '<%= jsRemark %>',
						},null,mainData);
				}
				if (_that.subEditorObj['title8']) {
					changeEditor('title8', 'objStr', {
							titleTemplate: '<%= objName %>',
							subtitle1Template: '评审人：<%= userPostName %>',
							subtitle2Template: '结论：<%= resultName?resultName:"未选" %>',
						},null,mainData);
				}
				//审核修改
				if(fileId){
					//评审结论相关信息
					changeEditor('title11', 'resultId', {
						data:configSelectData(res[2],'id','dicname'),
						defaultValue: res[3].value.resultId,
						defaultName: res[3].value.resultName
					},null,mainData);
						
					var fieldArr = getFormFields(mainData)
					fieldArr.push("id")
					delete res[3].value.userList
					var fileList = _that._.cloneDeep(res[3].value.fileList)
					_that.subEditorObj["title6"].workList = fileList
					_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "fileStr", JSON.stringify(fileList))
					_that.$set(_that.formdata, "fileStr", JSON.stringify(fileList))
					delete res[3].value.fileList
					if(_that.subEditorObj["title6"]){
						changeEditor("title6", "fileStr", {
							showDel:false,
							showAdd:false
						}, null, mainData)
					}
					
					var traitList = _that._.cloneDeep(res[3].value.traitList)
					_that.subEditorObj["title7"].workList = traitList
					_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "traitStr", JSON.stringify(traitList))
					_that.$set(_that.formdata, "traitStr", JSON.stringify(traitList))
					delete res[3].value.traitList
					if(_that.subEditorObj["title7"]){
						changeEditor("title7", "traitStr", {
							showDel:false,
							showAdd:false
						}, null, mainData)
					}
					
					var objList = _that._.cloneDeep(res[3].value.objList)
					_that.subEditorObj["title8"].workList = objList
					_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "objStr", JSON.stringify(objList))
					_that.$set(_that.formdata, "objStr", JSON.stringify(objList))
					delete res[3].value.objList
					if(_that.subEditorObj["title8"]){
						changeEditor("title8", "objStr", {
							showDel:false,
							showAdd:false
						}, null, mainData)
					}
					for(var i in res[3].value){
						if(fieldArr.includes(i)){
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata, i, res[3].value[i])
							_that.$set(_that.formdata, i, res[3].value[i])
						}
					}
				}
			}, options);
		}
	},
	onLoad(options) {
		let _that = this;
		_that.initLoad(options);
	},
	onShow() {
		let _that = this;
		if (_that.$refs.autoform) {
			for (var fieldId in _that.subEditorObj) {
				var editor = _that.subEditorObj[fieldId];
				uni.getStorage({
					key: getWorkCacheKey(fieldId),
					success: function(res) {
						editor.workList = res.data;
						//评审所需提供的交付物信息
						if(fieldId == 'title6'){
							//零组件特性(技术要求)分析，加载图号
							let drawingCodes = res.data.filter(e=>{
								if(e){
									return true
								}else{
									return false
								}
							}).map(e=>{
								return {name:e.drawingCode}
							});
							changeEditor('title7','drawingCode',{
								data:configSelectData(drawingCodes,'name','name')
							},null,_that.subConfigData["title7"])
						}
						uni.removeStorage({
							key: getWorkCacheKey(fieldId)
						});
					}
				});
			}
			uni.getStorage({
				key: _that.projectCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data;
						var formdata = _that.$refs.autoform.formdata;
						formdata.projectId = d.id
						formdata.projectCode = d.projectCode
						formdata.projectName =  d.projectName
						formdata.userId = d.userId
						formdata.userName = d.userName
						formdata.postId = d.postId
						formdata.postName = d.postName
						formdata.instId = d.instId
						formdata.instName = d.instName
						if( d.partakePostIdStr){
							formdata.partakeUserIdStr = d.partakePostIdStr
							formdata.partakeUserNameStr = d.partakePostNameStr
						}
						
						uni.removeStorage({
							key: _that.projectCacheKey
						});
					}
				}
			});
			uni.getStorage({
				key: _that.reviewLeaderCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data;
						var formdata = _that.$refs.autoform.formdata;
						formdata.reviewLeaderId = d.userId;
						formdata.reviewLeaderName = d.userName;
						formdata.reviewLeaderPostId = d.posId
						formdata.reviewLeaderPostName = d.posName
						formdata.reviewLeaderInstId = d.instId
						formdata.reviewLeaderInstName = d.instName
						uni.removeStorage({
							key: _that.reviewLeaderCacheKey
						});
					}
				}
			});
			uni.getStorage({
				key: _that.partakeUserCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data;
						var formdata = _that.$refs.autoform.formdata;
						formdata.partakeUserIdStr = d.map(e=>e.id).join(",")
						formdata.partakeUserNameStr = d.map(e=>e.username).join(",");
						_that.$refs.autoform.changeEditor('title1','partakeUserIdStr',{
							initSelect:d
						})
						uni.removeStorage({
							key: _that.partakeUserCacheKey
						});
					}
				}
			});
		}
	}
};
</script>
<style></style>
